package com.zcy.pmp.service.impl;

import com.zcy.basic.service.impl.BaseServiceImpl;
import com.zcy.pmp.domain.Department;
import com.zcy.pmp.mapper.DepartmentMapper;
import com.zcy.pmp.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2023/11/29 15:21
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void update(Department department) {
        if (department.getParent() == null || department.getParent().getId() == null) {
            department.setPath("/" + department.getId());
        } else {
            department.setPath(departmentMapper.queryById(department.getParent().getId()).getPath() + "/" + department.getId());
        }
        System.out.println("\n================================ Service =================================");
        System.out.println(department);
        System.out.println("================================================================================\n");
        departmentMapper.update(department);
    }


    /**
     * 无限级部门树查询
     * @param  id
     * @return
     */
    @Override
    public List<Department> queryDepartmentTree(Serializable id) {
        List<Department> departments = departmentMapper.queryAllExcludeById(id);
        // 获取所有的一级部门
        List<Department> primaryDepartmentList = departments.stream()
                .filter(d -> d.getParent() == null)
                .collect(Collectors.toList());
        // 获取所有的非一级部门
        List<Department> childrenDepartmentList = departments.stream()
                .filter(d -> d.getParent() != null)
                .collect(Collectors.toList());

        primaryDepartmentList.forEach(d -> {
            setChildrenTree(d, childrenDepartmentList);
        });
//        return departmentMapper.queryDepartmentTree(id);
        return primaryDepartmentList;
    }

    /*private List<Department> getChildrenTree(Department root, List<Department> all) {
        List<Department> children = new ArrayList<>();
        for (Department department : all) {
            if (department.getParent().getId().equals(root.getId())) {
                children.add(department);
                children = getChildrenTree(department, all);
                root.setChildren(children);
            }
        }
        return children;
    }*/

    private void setChildrenTree(Department root, List<Department> all) {
        List<Department> children = new ArrayList<>();
        for (Department department : all) {
            if (department.getParent().getId().equals(root.getId())) {
                setChildrenTree(department, all);
                children.add(department);
            }
        }
        if (children.isEmpty())  children = null;
        root.setChildren(children);
    }
}
